﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KiemKeKho
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion
        public DateTime date;

        Entities.ChiTietKiemKeKho chitietkiemkekho;
        public DateTime ngaykiemke;

        #region xử lý object và xử lý dữ liệu
        public void XuLyDuLieu(Entities.KiemKeKho kiemkekho)
        {
            try
            {
                MaPhieuKiemKeKho = kiemkekho.MaPhieuKiemKe;
                ngaykiemke = kiemkekho.NgayKiemKe;
                txtMaKho.Text = kiemkekho.MaKho;
                txtTenKho.Text = kiemkekho.TenKho;
                txtGhichuKK.Text = kiemkekho.GhiChu;

            }
            catch { }
        }
        public Entities.KiemKeKho XuLyObject(string hanhdong)
        {
            try
            {
                kiemkekho = new Entities.KiemKeKho();
                kiemkekho.HanhDong = hanhdong;
                kiemkekho.MaPhieuKiemKe = MaPhieuKiemKeKho;
                kiemkekho.NgayKiemKe = ngaykiemke;
                kiemkekho.MaKho = makho;
                kiemkekho.TenKho = tenkho;
                kiemkekho.GhiChu = txtGhichuKK.Text;
                return kiemkekho;
                return null;
            }
            catch
            {
                return null;
            }

        }
        private void txtTonThucTe_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = int.Parse(txtTonThucTe.Text);
                if (i < 0)
                {
                    txtTonThucTe.Text = "";
                }
            }
            catch
            {
                txtTonThucTe.Text = "";
            }
        }

        private void txtTonSoSach_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = int.Parse(txtTonSoSach.Text);
                if (i < 0)
                {
                    txtTonSoSach.Text = "";
                }
            }
            catch
            {
                txtTonSoSach.Text = "";
            }
        }
        #endregion

        #region khai báo khởi tạo
        public bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.KiemKeKho kiemkekho;
        Entities.KiemKeKho temp;
        Entities.ChiTietKiemKeKho[] hienthi;
        Entities.ChiTietKiemKeKho[] tempcheck;
        Entities.ChiTietKhoHang[] chitietkhohang;
        Entities.HangHoa[] hanghoa;
        private string MaPhieuKiemKeKho = "";
        private DateTime NgayKiemKe;
        private string tenhanghoa = "";
        private int tonsosach;
        private int tonthucte;
        private string ghichu;
        string makho = "";
        string tenkho = "";
        string hanhDong;

        public frmXuLy()
        {
            InitializeComponent();
            Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
            makho = kho.MaKho;
            tenkho = kho.TenKho;
            Entities.ChiTietKhoHang tem = new Entities.ChiTietKhoHang();
            tem.MaKho = makho;
            tem.HanhDong = "ChiTietHangTheoKho";
            MaPhieuKiemKeKho = getID();
            NgayKiemKe = DateTime.Now;
            txtMaKho.Text = Common.Constants.MaKho + makho;
            txtTenKho.Text = Common.Constants.TenKho + tenkho;
            hanhDong = "Insert";
        }

        public frmXuLy(Entities.KiemKeKho kiemkekho)
        {
            InitializeComponent();
            Entities.CaiDatKho kho1 = GUI.Entitystatic.ViewFile();
            makho = kho1.MaKho;
            tenkho = kho1.TenKho;
            temp = kiemkekho;
            MaPhieuKiemKeKho = kiemkekho.MaPhieuKiemKe;
            txtMaKho.Text = Common.Constants.MaKho + makho;
            txtTenKho.Text = Common.Constants.TenKho + tenkho;
            hanhDong = "Update";
        }
        #endregion

        #region lấy id
        public string getID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "KiemKeKho";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "KKK_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "KKK_0001";
            }
            return code;
        }
        #endregion

        #region Xử lý thêm sửa
        void XulyThemSua()
        {
            if (hanhDong == "Insert")
                GhiLai();
            else
                Sua();
        }
        #endregion
        Entities.ChiTietKiemKeKho save;
        #region sửa row
        int i;
        void SuaHangHoa()
        {

            if (i < 0)
                return;
            if (dgvHienThi.RowCount > 1)
            {
                MaPhieuKiemKeKho = dgvHienThi["MaPhieuKiemKe", i].Value.ToString().ToUpper();
                txtMaHangHoa.Text = dgvHienThi["MaHangHoa", i].Value.ToString().ToUpper();
                txtTenHangHoa.Text = dgvHienThi["TenHangHoa", i].Value.ToString();
                txtTonSoSach.Text = dgvHienThi["TonSoSach", i].Value.ToString();
                txtTonThucTe.Text = dgvHienThi["TonThucTe", i].Value.ToString();
                txtGhiChu.Text = dgvHienThi["GhiChu", i].Value.ToString();
                tempcheck = new Entities.ChiTietKiemKeKho[dgvHienThi.RowCount - 1];
                int sotang = 0;
                for (int j = 0; j < dgvHienThi.RowCount; j++)
                {
                    if (dgvHienThi["MaHangHoa", j].Value.ToString() != dgvHienThi["MaHangHoa", i].Value.ToString())
                    {
                        tempcheck[sotang] = new Entities.ChiTietKiemKeKho("Delete", 0,
                 dgvHienThi["MaPhieuXuatHuy", j].Value.ToString(), dgvHienThi["MaHangHoa", j].Value.ToString(),
                 dgvHienThi["TenHangHoa", j].Value.ToString(), int.Parse(dgvHienThi["TonSoSach", j].Value.ToString()),
             int.Parse(dgvHienThi["TonThucTe", j].Value.ToString()), dgvHienThi["GhiChu", j].Value.ToString(), false);
                        sotang++;
                    }
                }
                dgvHienThi.DataSource = tempcheck;
            }
            else
            {
                MaPhieuKiemKeKho = dgvHienThi["MaPhieuXuatHuy", i].Value.ToString().ToUpper();
                txtMaHangHoa.Text = dgvHienThi["MaHangHoa", i].Value.ToString().ToUpper();
                txtTenHangHoa.Text = dgvHienThi["TenHangHoa", i].Value.ToString();
                txtTonSoSach.Text = dgvHienThi["TonSoSach", i].Value.ToString();
                txtTonThucTe.Text = dgvHienThi["TonThucTe", i].Value.ToString();
                txtGhiChu.Text = dgvHienThi["GhiChu", i].Value.ToString();
                dgvHienThi.DataSource = new Entities.ChiTietKiemKeKho[0];
            }
        }
        #endregion

        #region kiểm tra
        public bool Validate()
        {
            if (txtMaHangHoa.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lblMaHangHoa.Text.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtMaHangHoa.Focus();
                return false;
            }
            if (txtTenHangHoa.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lblTenHangHoa.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtTenHangHoa.Focus();
                return false;
            }
            if (txtTonSoSach.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lblTonSoSach.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtTonSoSach.Focus();
                return false;
            }
            if (txtTonThucTe.Text == "")
            {
                MessageBox.Show(Common.MSG.MSG13(lblTonThucTe.ToString()), Common.MSG.TieuDe, MessageBoxButtons.OK);
                txtTonThucTe.Focus();
                return false;
            }
            return true;
        }
        #endregion

        #region Thêm Mới LV
        public void NewRow()
        {
            try
            {
                bool kt1 = false;

                int sohangtrongbang;
                hienthi = (Entities.ChiTietKiemKeKho[])dgvHienThi.DataSource;
                // list view = null
                if (hienthi == null)
                {

                    tempcheck = new Entities.ChiTietKiemKeKho[1];
                    tempcheck[0] = new Entities.ChiTietKiemKeKho("Insert", 0, MaPhieuKiemKeKho, txtMaHangHoa.Text, txtTenHangHoa.Text, int.Parse(txtTonThucTe.Text.ToString()), int.Parse(txtTonSoSach.Text.ToString()), txtGhiChu.Text, false);
                    dgvHienThi.DataSource = tempcheck;
                    return;
                }

                sohangtrongbang = hienthi.Length;
                // số lượng bảng ghi khác 0
                if (sohangtrongbang != 0)
                {
                    for (int j = 0; j < sohangtrongbang; j++)
                    {
                        // nếu tìm được mã đã có trong list view
                        if (mahanghoa == hienthi[j].MaHangHoa)
                        {
                            tempcheck = new Entities.ChiTietKiemKeKho[sohangtrongbang];
                            break;
                        }
                        // nếu không tìm được
                        else
                            tempcheck = new Entities.ChiTietKiemKeKho[sohangtrongbang + 1];
                    }
                }
                // số lượng bảng ghi = 0
                else
                    tempcheck = new Entities.ChiTietKiemKeKho[sohangtrongbang + 1];
                // nếu số lượng bảng ghi khác 0
                if (tempcheck.Length != 0)
                {
                    try
                    {
                        // số lượng bảng ghi = số lượng row list view
                        if (tempcheck.Length == hienthi.Length)
                        {
                            for (int j = 0; j < tempcheck.Length; j++)
                            {
                                // nếu mã = mã trong list view
                                if (mahanghoa == hienthi[j].MaHangHoa)
                                {
                                    //
                                    tempcheck[j] = new Entities.ChiTietKiemKeKho("Insert", 0, MaPhieuKiemKeKho, txtMaHangHoa.Text, txtTenHangHoa.Text, int.Parse(txtTonThucTe.Text.ToString()), int.Parse(txtTonSoSach.Text.ToString()), txtGhiChu.Text, false);
                                }
                                // nếu mã không = mã trong list view
                                else
                                    tempcheck[j] = new Entities.ChiTietKiemKeKho("Insert", hienthi[j].ChiTietKiemKeKhoID, hienthi[j].MaPhieuKiemKe, hienthi[j].MaHangHoa, hienthi[j].TenHangHoa, hienthi[j].TonThucTe, hienthi[j].TonSoSach, hienthi[j].LyDo, hienthi[j].Deleted);

                            }
                        }
                        // số lượng bảng ghi không = số lượng row list view
                        else
                        {
                            for (int j = 0; j < tempcheck.Length; j++)
                            {
                                // lấy lại số bảng ghi trên list view
                                if (j < tempcheck.Length - 1)
                                    tempcheck[j] = new Entities.ChiTietKiemKeKho("Insert", hienthi[j].ChiTietKiemKeKhoID, hienthi[j].MaPhieuKiemKe, hienthi[j].MaHangHoa, hienthi[j].TenHangHoa, hienthi[j].TonThucTe, hienthi[j].TonSoSach, hienthi[j].LyDo, hienthi[j].Deleted);
                                // bảng ghi cuối cùng là mới insert vào
                                else
                                {
                                    tempcheck[tempcheck.Length - 1] = new Entities.ChiTietKiemKeKho("Insert", 0, MaPhieuKiemKeKho, txtMaHangHoa.Text, txtTenHangHoa.Text, int.Parse(txtTonThucTe.Text.ToString()), int.Parse(txtTonSoSach.Text.ToString()), txtGhiChu.Text, false);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {

                        tempcheck[tempcheck.Length - 1] = new Entities.ChiTietKiemKeKho("Insert", 0, MaPhieuKiemKeKho, txtMaHangHoa.Text, txtTenHangHoa.Text, int.Parse(txtTonThucTe.Text.ToString()), int.Parse(txtTonSoSach.Text.ToString()), txtGhiChu.Text, false);
                    }
                    dgvHienThi.DataSource = tempcheck;
                }
            }

            catch
            {

                tempcheck = new Entities.ChiTietKiemKeKho[1];
                tempcheck[0] = new Entities.ChiTietKiemKeKho("Insert", 0, MaPhieuKiemKeKho, txtMaHangHoa.Text, txtTenHangHoa.Text, int.Parse(txtTonThucTe.Text.ToString()), int.Parse(txtTonSoSach.Text.ToString()), txtGhiChu.Text, false);
                dgvHienThi.DataSource = tempcheck;
            }
            finally
            {

            }
        }
        #endregion
        string mahanghoa;
        #region Ghi Lại
        void GhiLai()
        {
            try
            {
                if (check)
                {
                    check = false;
                    return;
                }
                ngaykiemke = DateTime.Now;
                kiemkekho = XuLyObject("Insert");
                tempcheck = (Entities.ChiTietKiemKeKho[])dgvHienThi.DataSource;
                // check object
                if (kiemkekho == null)
                {
                    MessageBox.Show(Common.MSG.MSG35(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check object
                if (tempcheck == null || tempcheck.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG35(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                //// check conflic insert
                if (!CheckConflicInsert(kiemkekho))
                {
                    MessageBox.Show(Common.MSG.MSG5(kiemkekho.MaPhieuKiemKe), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    MaPhieuKiemKeKho = makiemkekho;
                    Select("");
                    return;
                }
                // check insert
                if (Common.StaticVariable.InterfaceObject.InsertUpdateKiemKeKho(kiemkekho) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(kiemkekho.MaPhieuKiemKe), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    // UtilitiesVariable.client.Delete(kiemkekho);
                    return;
                }

                // check insert details
                for (int i = 0; i < tempcheck.Length; i++)
                {
                    tempcheck[i].MaPhieuKiemKe = MaPhieuKiemKeKho;
                }
                if ( Common.StaticVariable.InterfaceObject.InsertUpdateChiTietKiemKeKho(tempcheck) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(kiemkekho.MaPhieuKiemKe), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    Entities.ChiTietKiemKeKho delete = new Entities.ChiTietKiemKeKho();
                    delete.MaPhieuKiemKe = kiemkekho.MaPhieuKiemKe;
                    //BusinessLogic.ChiTietKiemKeKho.Delete(delete);
                    Common.StaticVariable.InterfaceObject.DeleteChiTietKiemKeKho(delete);
                    return;
                }
                checkClose = true;
                kiemkekho = null;
                check = true;
                GetData(true);
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Insert
        string makiemkekho;
        public bool CheckConflicInsert(Entities.KiemKeKho kiemkekho)
        {
            makiemkekho = getID();
            if (kiemkekho.MaPhieuKiemKe == makiemkekho)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {

                if (check)
                {
                    check = false;
                    return;
                }
                kiemkekho = XuLyObject("Update");
                tempcheck = (Entities.ChiTietKiemKeKho[])dgvHienThi.DataSource;
                // check object
                if (kiemkekho == null)
                {
                    return;
                }
                // check object
                if (tempcheck == null || tempcheck.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG35(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check conflic insert
                if (!CheckConflicUpdate())
                {
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check insert
                if ( Common.StaticVariable.InterfaceObject.InsertUpdateKiemKeKho(kiemkekho) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG17(kiemkekho.MaPhieuKiemKe), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // delete details
                Entities.ChiTietKiemKeKho delete = new Entities.ChiTietKiemKeKho();
                delete.MaPhieuKiemKe = kiemkekho.MaPhieuKiemKe;
                if ( Common.StaticVariable.InterfaceObject.DeleteChiTietKiemKeKho(delete) == 0)
                {

                }
                // check insert details
                foreach (Entities.ChiTietKiemKeKho item in tempcheck)
                {
                    item.HanhDong = "Insert";
                }
                if (Common.StaticVariable.InterfaceObject.InsertUpdateChiTietKiemKeKho(tempcheck) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(kiemkekho.MaPhieuKiemKe), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    Entities.ChiTietKiemKeKho deleteerror = new Entities.ChiTietKiemKeKho();
                    deleteerror.MaPhieuKiemKe = MaPhieuKiemKeKho;
                    Common.StaticVariable.InterfaceObject.DeleteChiTietKiemKeKho(deleteerror);
                    return;
                }

                checkClose = true;
                kiemkekho = null;
                check = true;
                GetData(true);
                this.Close();

            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Update
        public bool CheckConflicUpdate()
        {
            try
            {
                Entities.KiemKeKho[] mangchitiet = Common.StaticVariable.InterfaceObject.SelectKiemKeKho(kiemkekho); //new ucHienThiQuanLy().HienThi(makho);
                for (int i = 0; i < mangchitiet.Length; i++)
                {
                    if (mangchitiet[i].MaPhieuKiemKe == temp.MaPhieuKiemKe)
                    {
                        return Check(mangchitiet[i]);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Check(Entities.KiemKeKho kiemkekho)
        {
            bool kt = true;
            return kt;
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.checkClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion



        #region xử lý form
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {

                if (this.hanhDong == "Update")
                {
                        dgvHienThi.Enabled = false;
                        btnGhiLai.Enabled = false;
                }
                txtMaKho.Focus();
                this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
                HienThi();
            }
            catch
            {
            }
        }
        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                i = e.RowIndex;
            }
            catch { }
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SuaHangHoa();
            }
            catch { }
        }
        void txtMaHH_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.F4)
                {
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                        return;
                    }

                    TimKiem.frmHangHoa timkiem = new TimKiem.frmHangHoa(makho);
                    timkiem.ShowDialog();
                    if (TimKiem.frmHangHoa.selectedItem != null)
                    {
                        txtMaHangHoa.Text = mahanghoa = TimKiem.frmHangHoa.selectedItem.MaHangHoa;
                        txtTenHangHoa.Text = TimKiem.frmHangHoa.selectedItem.TenHangHoa;
                        txtTonSoSach.Text = TimKiem.frmHangHoa.selectedItem.SoLuong.ToString();
                        txtTonThucTe.Focus();
                        TimKiem.frmHangHoa.selectedItem = null;
                    }
                }
            }
            catch
            {
            }
        }
        void txtGhiChu_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Enter)
            {
                if (Validate())
                {
                    mahanghoa = txtMaHangHoa.Text;
                    tenhanghoa = txtTenHangHoa.Text;
                    tonthucte = int.Parse(txtTonThucTe.Text);
                    tonsosach = int.Parse(txtTonSoSach.Text);
                    ghichu = txtGhiChu.Text;
                    NewRow();
                    txtMaHangHoa.Text = null;
                    txtTenHangHoa.Text = null;
                    txtTonThucTe.Text = null;
                    txtTonSoSach.Text = null;
                    txtGhiChu.Text = null;
                    txtMaHangHoa.Focus();
                }
            }
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }

        public delegate void SelectData(string hanhdong);
        public SelectData Select;
        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.F2)
                {
                    XulyThemSua();
                }

                if (e.KeyData == Keys.Escape)
                {
                    Thoat();
                }
            }
            catch
            {
            }
        }

        #endregion

        private void btnGhiLai_Click(object sender, EventArgs e)
        {

            XulyThemSua();
        }
        #region Processing
        private Entities.ChiTietKiemKeKho[] DanhSach;
        private Entities.ChiTietKiemKeKho GoiHang = null;
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaHangHoa == this.mahanghoa)
                    {
                        GoiHang = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region hiển thị
        private void HienThi()
        {
            try
            {
                Entities.ChiTietKiemKeKho nv = new Entities.ChiTietKiemKeKho();
                nv.HanhDong = "3";
                nv.MaPhieuKiemKe = MaPhieuKiemKeKho;
                DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietKiemKeKho(nv);
                dgvHienThi.DataSource = DanhSach;
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                DanhSach = null;
                HienThiListview();
            }
        }

        private void HienThiListview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.ChiTietKiemKeKho[] nv = new Entities.ChiTietKiemKeKho[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }

                dgvHienThi.Columns["MaHangHoa"].HeaderText = "mã hàng hóa";
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["TonSoSach"].HeaderText = "Tồn Sổ Sách";
                dgvHienThi.Columns["TonSoSach"].Visible = true;
                dgvHienThi.Columns["TonThucTe"].HeaderText = "Tồn Thực Tế";
                dgvHienThi.Columns["TonThucTe"].Visible = true;
                dgvHienThi.Columns["LyDo"].HeaderText = "Ghi Chú";
                dgvHienThi.Columns["LyDo"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion 

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            NewRow();
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
            txtTonSoSach.Text = "";
            txtTonThucTe.Text = "";
            txtGhiChu.Text = "";
        }

    }
}
